package edu.xpu.project.common.interception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import edu.xpu.project.user.entity.ProjectUser;

/**
 * @ClassName: LoginInterception.java
 * @Description: 用户登陆拦截器, 判断用户是否登陆
 * 			对所有的资源进行保护
 *
 * @author liukang
 * @date 2017年5月13日 上午9:53:17
 * @version 
 * @since JDK 1.6
 */
public class LoginInterception implements HandlerInterceptor {

	private final static String loginUrl = "/schoolbus/index.jsp";
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		//获取session中得对象
		HttpSession session = request.getSession();
		ProjectUser projectUser = (ProjectUser) session.getAttribute("sessionUser");
		//用户没有登陆
		if (projectUser == null) {
			response.sendRedirect(loginUrl + "?errMsg=");
			return false;
		}
		return true;
	}

}

